//
//  HMSinaTabBarController.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/12.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit

class HMSinaTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabbar
        let hmSinaTabBar = HMSinaTabBar()
        //只读属性不能直接赋值
        self.setValue(hmSinaTabBar, forKey: "tabBar")
        
        addChildViewControllers()
        
        //监听点击事件，有循环引用 
        hmSinaTabBar.addButtonClosure = {[weak self] in
        
            //实际开发当中，按钮的点击事件处理的地方，需要留一个查找的标记
            print("按钮的点击事件")
            print(self!)
            
            //显示截取的图片
            
            let addButtonclosureView = HMSinaAddButtonClosureView()
            
//            addButtonclosureView.show(fromViewController: self)
            
            UIApplication.shared.keyWindow?.addSubview(addButtonclosureView)
        
        }
    }
    
    private func addChildViewControllers() {
        addChildViewController(viewController: HMSinaHomeTableViewController(), title: "首页", imageName: "tabbar_home" , index : 0)
        addChildViewController(viewController: HMSinaDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover" , index : 1)
        addChildViewController(viewController: HMSinaMessageTableViewController(), title: "消息", imageName: "tabbar_message_center" , index : 2)
        addChildViewController(viewController: HMSinaProfileTableViewController(), title: "个人", imageName: "tabbar_profile" , index : 3)
    }

    private func addChildViewController(viewController : UIViewController , title : String , imageName : String , index : Int) {
        
        
        //设置title，同时这是title和图片才能显示
        viewController.tabBarItem.image = UIImage(named:imageName)
        
        viewController.tabBarItem.tag = index
        
        //设置选中的image
        viewController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        //设置文字的颜色
        viewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        //设置文字字体
        viewController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        //设置文字的偏移
        viewController.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0)
        //设置消息提醒
        viewController.tabBarItem.badgeValue = "3"
        if #available(iOS 10.0, *) {
            viewController.tabBarItem.badgeColor = UIColor.red
        } else {
            
        }
        //分别设置navigationBar和tabbar的title
        viewController.navigationItem.title = title
        viewController.tabBarItem.title = title
        
        //设置图片的偏移
        viewController.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        
        //在导航控制器中添加tableViewController
        let navigationViewController = HMSinaBaseNavigationController(rootViewController: viewController)
        
        //将导航控制器添加到tabBarController
        self.addChildViewController(navigationViewController)
        
    }
    
    //自定义首页tabbar点击的动画
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        //1、应该获取到被点击的UITabBarButton
        var index = 0
        
        for subView in tabBar.subviews {
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag {
                    
                    //可以获取到被点击的tabbatButton
                    //遍历子视图
                    for View in subView.subviews{
                    
                        if View .isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            View.transform = CGAffineTransform.init(scaleX: 0.7, y: 0.7)
                            
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: [], animations: {
                                
                                View.transform = CGAffineTransform.identity
                                
                            }, completion: { (Bool) in
                                
                            })
                            
                        }
                    
                    }
                    
                }
                index += 1
            }
            
        }
        
    }

}
